﻿Imports System.Activities
Imports System.Data.SqlClient
Imports System.Activities.Presentation.Metadata
Imports System.ComponentModel
Imports System.Drawing

Public Class ReOrganizeIndexActivity
    Inherits MSSQLBaseActivity

    Public Property IndexName As InArgument(Of String)
    Public Property TableName As InArgument(Of String)

    Public Sub New()
        Me.DisplayName = My.Resources.ReOrganizeIndexActivityDisplayName
    End Sub

    Protected Overrides Function BeginExecute(context As System.Activities.AsyncCodeActivityContext, callback As System.AsyncCallback, state As Object) As System.IAsyncResult
        Dim tableName = context.GetValue(Me.TableName)
        Dim indexName = context.GetValue(Me.IndexName)
        Dim conn = context.GetValue(Me.ConnectionStrings)
        Dim useLog = context.GetValue(Me.OutSQLLog)
        Dim asyncExecute = New asyncExecuteDelegate(AddressOf ExecuteSQL)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(CreateSQLStrings(tableName, indexName), conn, True, useLog, callback, state)
    End Function

    Public Overrides Sub Register()
        MyBase.Register()

        Dim build As New AttributeTableBuilder
        'ReOrganizeIndexActivity Activity
        build.AddCustomAttributes(GetType(ReOrganizeIndexActivity),
                                              New ToolboxBitmapAttribute(GetType(ReOrganizeIndexActivity), "ReOrganizeIndex.png"))
        build.AddCustomAttributes(GetType(ReOrganizeIndexActivity), "IndexName",
                                              New RequiredArgumentAttribute,
                                              New CategoryAttribute(My.Resources.CategoryDetail),
                                              New DisplayNameAttribute(My.Resources.IndexNameDisplay),
                                              New DescriptionAttribute(My.Resources.IndexNameDescription))
        build.AddCustomAttributes(GetType(ReOrganizeIndexActivity), "TableName",
                                              New RequiredArgumentAttribute,
                                              New CategoryAttribute(My.Resources.CategoryDetail),
                                              New DisplayNameAttribute(My.Resources.TableNameDisplay),
                                              New DescriptionAttribute(My.Resources.TableNameDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub

    Protected Overrides Function ExecuteSQL(script As String, connect As String, needRes As Boolean, ByVal needLog As Boolean) As System.Data.DataSet
        Dim sqlExec As New SQLExecute With {.Timeout = 0}
        sqlExec.ExecuteSQL(script, connect, needRes)

        Return sqlExec.ConvertMessageToDataset(sqlExec.InfoMessages)
    End Function

    ''' <summary>実行する SQL の生成</summary>
    ''' <remarks>http://msdn.microsoft.com/ja-jp/library/ms189858(SQL.90).aspx</remarks>
    Private Function CreateSQLStrings(ByVal tableName As String, ByVal indexName As String) As String
        Dim sqlBuilder As New System.Text.StringBuilder
        With sqlBuilder
            .Append("ALTER INDEX ")
            .Append(indexName + " ")
            .Append("ON ")
            .Append(tableName + " ")
            .Append("REORGANIZE ")
            .Append(";")
        End With
        Return sqlBuilder.ToString
    End Function

End Class
